﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Input;
using EnergyConsumptionMonitorSystem.Components;
using EnergyConsumptionMonitorSystem.MainWindowTabItem;
using EnergyConsumptionMonitorSystem.Models;
using System.Windows.Media;
using System.Windows;
using System.Configuration;

namespace EnergyConsumptionMonitorSystem.MainWindow
{
    class MainWindowViewModel : INotifyPropertyChanged
    {

        private ObservableCollection<MainWindowTabItemViewModel> _tabItemViewModels = new ObservableCollection<MainWindowTabItemViewModel>();
        public ObservableCollection<MainWindowTabItemViewModel> TabItemViewModels
        {
            get { return this._tabItemViewModels; }
            set
            {
                if (this._tabItemViewModels != value)
                {
                    this._tabItemViewModels = value;
                    NotifyPropertyChanged("TabItemViewModels");
                }
            }
        }

        public bool LogInStatus { get { return Singleton<LogInStatus>.Instance.Status; } }

        private MainWindowTabItem.MainWindowTabItem _selectedItem;
        public MainWindowTabItem.MainWindowTabItem SelectedItem
        {
            get
            {
                return this._selectedItem;
            }
            set
            {
                if (this._selectedItem != value)
                {
                    this._selectedItem = value;
                    NotifyPropertyChanged("SelectedItem");
                }
            }
        }

        private string _userWelcomeText;
        public string UserWelcomeText
        {
            get { return this._userWelcomeText; }
            set
            {
                if (this._userWelcomeText != value)
                {
                    this._userWelcomeText = value;
                    NotifyPropertyChanged("UserWelcomeText");
                }
            }
        }


        private string _userWelcomeName;
        public string UserWelcomeName
        {
            get { return this._userWelcomeName; }
            set
            {
                if (this._userWelcomeName != value)
                {
                    this._userWelcomeName = value;
                    NotifyPropertyChanged("UserWelcomeName");
                }
            }
        }

        private ICommand _personalInfoCommand;
        public ICommand PersonalInfoCommand { get { return this._personalInfoCommand; } }

        private ICommand _typeInCommand;
        public ICommand TypeInCommand { get { return this._typeInCommand; } }

        private ICommand _calculateCommand;
        public ICommand CalculateCommand { get { return this._calculateCommand; } }

        private ICommand _logOutCommand;
        public ICommand LogOutCommand { get { return this._logOutCommand; } }

        private MainWindowTabItem.MainWindowTabItem _tempTabItem;

        public MainWindowViewModel()
        {
            this._personalInfoCommand = new Command(new Action<object>(this.PersonalInfoClick));
            this._typeInCommand = new Command(new Action<object>(this.TypeInClick));
            this._calculateCommand = new Command(new Action<object>(this.CalculateClick));
            this._logOutCommand = new Command(new Action<object>(this.LogOut));
            Singleton<LogInStatus>.Instance.StatusChangedEvent += new Models.LogInStatus.StatusChangedHandler(LogInStatusChange);
            LogIn();
        }

        private void LogInStatusChange(bool value)
        {
            NotifyPropertyChanged("LogInStatus");
            if (value)
            {
                this._tabItemViewModels.Clear();
                NotifyPropertyChanged("TabItemViewModels");
                this.SelectedItem = CreateNewMainWindowTabItem<IntroductionUCViewModel>("Introduction", "介绍");
                this.UserWelcomeText = "欢迎您，";
                this.UserWelcomeName = Singleton<LogInStatus>.Instance.User.UserName;
            }
            else
            {
                this.UserWelcomeText = "未登录";
                this.UserWelcomeName = "";
                LogIn();
            }
        }

        private void LogIn()
        {
            MainWindowTabItemViewModel tabItemVM = new MainWindowTabItemViewModel();
            tabItemVM.HeaderText = "登录";
            tabItemVM.ItemID = "LogIn";
            tabItemVM.View.HeaderMouseLeaveEvent += new MouseEventHandler(tabItem_HeaderMouseLeaveEvent);
            tabItemVM.View.HeaderMouseEnterForAWhileEvent += new EventHandler(tabItem_HeaderMouseEnterForAWhileEvent);
            tabItemVM.View.PreviewHeaderMouseLeftButtonDownEvent += new MouseButtonEventHandler(tabItem_PreviewHeaderMouseLeftButtonDownEvent);
            LogInUCViewModel logInVM = new LogInUCViewModel();
            logInVM.RegisterEvent += new EventHandler(logIn_RegisterEvent);
            tabItemVM.ContentVM = logInVM;
            this._tabItemViewModels.Add(tabItemVM);
            NotifyPropertyChanged("TabItemViewModels");
            this.SelectedItem = tabItemVM.View;
        }

        private void LogOut(object o)
        {
            this._tabItemViewModels.Clear();
            NotifyPropertyChanged("TabItemViewModels");
            Singleton<LogInStatus>.Instance.LogOut();
        }

        private void logIn_RegisterEvent(object sender, EventArgs e)
        {
            this.SelectedItem = CreateNewMainWindowTabItem<RegisterUCViewModel>("Register", "注册");
        }

        private void PersonalInfoClick(object o)
        {
            this.SelectedItem = CreateNewMainWindowTabItem<PersonalInfoUCViewModel>("PersonalInfo", "个人信息");
        }

        private void TypeInClick(object o)
        {
            this.SelectedItem = CreateNewMainWindowTabItem<TypeInUCViewModel>("TypeIn", "能量摄入录入");
        }

        private void CalculateClick(object o)
        {
            this.SelectedItem = CreateNewMainWindowTabItem<CalculateUCViewModel>("Calculate", "能量消耗计算");
        }

        private MainWindowTabItem.MainWindowTabItem CreateNewMainWindowTabItem<T>(string itemID, string headerText) where T : TabItemContentUCViewModel, new()
        {
            foreach (MainWindowTabItemViewModel vm in this._tabItemViewModels.ToArray())
            {
                if (vm.ItemID == itemID)
                {
                    return vm.View;
                }
            }
            MainWindowTabItemViewModel tabItemVM = new MainWindowTabItemViewModel();
            tabItemVM.HeaderText = headerText;
            tabItemVM.ItemID = itemID;
            tabItemVM.CloseRequestEvent += new MainWindowTabItemViewModel.CloseRequestHandler(tabItemVM_CloseRequestEvent);
            tabItemVM.View.HeaderMouseLeaveEvent += new MouseEventHandler(tabItem_HeaderMouseLeaveEvent);
            tabItemVM.View.HeaderMouseEnterForAWhileEvent += new EventHandler(tabItem_HeaderMouseEnterForAWhileEvent);
            tabItemVM.View.PreviewHeaderMouseLeftButtonDownEvent += new MouseButtonEventHandler(tabItem_PreviewHeaderMouseLeftButtonDownEvent);
            TabItemContentUCViewModel contentVM = new T();
            tabItemVM.ContentVM = contentVM;
            TabItemViewModels.Add(tabItemVM);
            NotifyPropertyChanged("TabItemViewModels");
            return tabItemVM.View;
        }

        private void tabItem_PreviewHeaderMouseLeftButtonDownEvent(object sender, MouseButtonEventArgs e)
        {
            this._tempTabItem = null;
        }

        private void tabItem_HeaderMouseEnterForAWhileEvent(object sender, EventArgs e)
        {
            if (this._tempTabItem == null)
            {
                this._tempTabItem = this.SelectedItem;
                this.SelectedItem = (MainWindowTabItem.MainWindowTabItem)sender;
            }
        }

        private void tabItem_HeaderMouseLeaveEvent(object sender, MouseEventArgs e)
        {
            if (this._tempTabItem != null)
            {
                this.SelectedItem = this._tempTabItem;
                this._tempTabItem = null;
            }
        }

        private void tabItemVM_CloseRequestEvent(string itemID)
        {
            foreach (MainWindowTabItemViewModel vm in this._tabItemViewModels.ToArray())
            {
                if (itemID == vm.ItemID)
                {
                    int index = this._tabItemViewModels.IndexOf(vm);
                    this._tabItemViewModels.Remove(vm);
                    if (this._tabItemViewModels.Count == 0)
                    {
                        this.SelectedItem = CreateNewMainWindowTabItem<IntroductionUCViewModel>("Introduction", "介绍");
                    }
                    else
                    {
                        if (this.SelectedItem == vm.View && this._tempTabItem == null)
                        {
                            if (index == 0)
                            {
                                this.SelectedItem = this._tabItemViewModels[0].View;
                            }
                            else
                            {
                                this.SelectedItem = this._tabItemViewModels[index - 1].View;
                            }
                        }
                    }
                    NotifyPropertyChanged("TabItemViewModels");
                    break;
                }
            }
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

    }
}
